<!-- 一周项目一览表 -->
<template>
	<view>
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">单日总库存</block></cu-custom>
		<view class="qiun-charts" >
			<!--#ifdef MP-ALIPAY -->
			<canvas canvas-id="canvasPie" id="canvasPie" class="charts" :width="cWidth*pixelRatio" :height="cHeight*pixelRatio" :style="{'width':cWidth+'px','height':cHeight+'px'}" @touchstart="touchPie"></canvas>
			<!--#endif-->
			<!--#ifndef MP-ALIPAY -->
			<canvas canvas-id="canvasPie" id="canvasPie" class="charts" @touchstart="touchPie"></canvas>
			<!--#endif-->
		</view>
		<view class="margin-top">
		   <z-table :tableData='tableData' :columns='columns'></z-table>
		</view>
	</view>
</template>

<script>
	import {
		formatDateDay
	} from '@/utils/datetime.js'
	import zTable from "@/components/z-table/z-table.vue"
	import uCharts from '@/common/lib/u-charts.min.js'
	var _self
	var canvaPie=null
	export default {
		components: {
			zTable
		},
		data() {
			return {
				tableData:[],
				columns:[
					{ title:'序号',key:'index',width: 100 },
					{ title:'货品类别',key:'name',width: 240 },
					{ title:'数量',key:'qty',width: 210  },
					{ title:'重量',key:'weight',width: 210  }
				],
				cWidth:'',
				cHeight:'',
				pixelRatio:1,
				textarea:''
			}
		},
		onLoad() {
			this.getTableData()
			
			_self = this
			//#ifdef MP-ALIPAY
			uni.getSystemInfo({
				success: function (res) {
					if(res.pixelRatio>1){
						//正常这里给2就行，如果pixelRatio=3性能会降低一点
						//_self.pixelRatio =res.pixelRatio;
						_self.pixelRatio =2
					}
				}
			});
			//#endif
			this.cWidth=uni.upx2px(750)
			this.cHeight=uni.upx2px(500)
		},
		methods: {
			getTableData(){				
				this.$minApi.getInStockGoodsDetailOnDayList().then(res => {
					const data = res
					if (data) {
						data.forEach( (item, index) => item.index = (index +1))
					}
					this.tableData = data
					this.getServerData()
				}).catch(err => {
					console.log(err)
				})
			},			
			getServerData(){
				const data = this.tableData.map(a => {
					return {
						name: a.name,
						data: a.weight
					}
				})
				const pieData = {
					series: data
				}

				let Pie={series:[]}
				//这里我后台返回的是数组，所以用等于，如果您后台返回的是单条数据，需要push进去
				Pie.series=pieData.series
				_self.textarea = JSON.stringify(pieData)
				_self.showPie("canvasPie",Pie)

			},
			showPie(canvasId,chartData){
				canvaPie=new uCharts({
					$this:_self,
					canvasId: canvasId,
					type: 'pie',
					fontSize:11,
					padding:[15,15,0,15],
					legend:{
						show:true,
						position:'right',
						padding:5,
						lineHeight:11,
						margin:0,
					},
					background:'#FFFFFF',
					pixelRatio:_self.pixelRatio,
					series: chartData.series,
					animation: true,
					width: _self.cWidth*_self.pixelRatio,
					height: _self.cHeight*_self.pixelRatio,
					dataLabel: true,
					extra: {
						pie: {
						  border:true,
						  borderColor:'#FFFFFF',
						  borderWidth:3
						}
					},
				})
			},
			touchPie(e){
				canvaPie.showToolTip(e, {
					format: function (item) {
						return item.name + ':' + item.data 
					}
				})
				canvaPie.touchLegend(e,{animation:true})
			},
			backedPage() {
				uni.redirectTo({
					url: `/pages/analysis/index`
				})
			}
		}
	}
</script>

<style>
	/* .project-list+.project-list {
		margin-top: 2rpx !important;
	} */
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	
	.charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
</style>
